﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;
namespace Web
{
    public partial class admin_career : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {
            BindModels();
            BindNotice();
        }
        private void BindModels()
        {
            List<CareerInfo> careerlist = CareerInfoLogic.GetAllCareerInfo();
            ListRpt.DataSource = careerlist;
            ListRpt.DataBind();
        }

        private void BindNotice()
        {
            noticesuccess.Visible = false;
            noticefailed.Visible = false;
        }
        protected void btnChange_Click(object sender, EventArgs e)
        {
            if (Request.Form["ckb"] == null)
            {
                return;
            }
            string[] careerIDs = Request.Form["ckb"].Split(',');
            int[] cids = new int[careerIDs.Count()];
            for (int i = 0; i < careerIDs.Count(); i++)
            {
                try
                {
                    cids[i] = Int32.Parse(careerIDs[i]);
                }
                catch { }
            }
            int count = 0;
            foreach (var id in cids)
            {
                count += CareerInfoLogic.DeleteCareerInfo(id);
            }
            if (count == careerIDs.Count())
            {
                ChangeStatus(true);

                //
                BindModels();
            }
            else
            {
                ChangeStatus(false);
            }
        }

        protected void ibtnDelete_OnClick(object sender, EventArgs e)
        {
            ImageButton ib = new ImageButton();
            int id = 0;
            try
            {
                ib = (ImageButton)sender;
                id = Int32.Parse(ib.ToolTip.ToString());
            }
            catch
            {
                return;
            }
            if (1 == CareerInfoLogic.DeleteCareerInfo(id))
            {
                ChangeStatus(true);

                //
                BindModels();
            }
            else
            {
                ChangeStatus(false);
            }
            //
            
        }
        private void ChangeStatus(bool flag)
        {
            noticesuccess.Visible = flag ? true : false;
            noticefailed.Visible = flag ? false : true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "initclosenotice", "AjaxChange();", true);
        }
        
    }
}
